package com.smartmail.receiving;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

import android.content.Context;

import com.smartmail.accounts.MailAccount;
import com.smartmail.mailing.MailMessage;

/**
 * This abstract class if a framework for two classes -
 * 
 * - POP3RetrievalThread
 * - IMAPRetrievalThread
 * 
 * implementations in appropriate package (pop3, imap)
 * 
 * AbstractRetrievalThread object is object connecting with mailbox.
 * Each object of this class has ScheduledExecutorService (getScheduler method)
 * object to which you can send a request to perform tasks on Mailbox.
 * 
 * These tasks can be stopped and resumed by appropriate methods ( stopThread, resumeThread)
 * 
 * @author Tomek
 */

public abstract class AbstractRetrievalThread  {


	/** 
	 * Public Constructor.
	 * 
	 * @param account - Mailbox Account where task will be performed.
	 * @param manager - Database manager which stores information about this account.
	 * @param context - Application context
	 */
	public AbstractRetrievalThread(MailAccount account,IncomingMailDBManager manager,Context context){
		
		_mailAccount = account;
		_dbManager = manager;
		_context = context;
		_messagesToDownload = new ArrayList<MailMessage>();
	}
	
	/**
	 * Force thread to check mail box.
	 */
	public abstract void checkMailbox();
	
	/**
	 * Force 'retrieval thread' to download message
	 * on demand ( only POP3 protocol ).
	 */
	public abstract void downloadMessages();

	/**
	 * Resume execution of tasks for this thread
	 * 
	 * @return - true on success, otherwise false.
	 */
	public abstract boolean resumeThread();
	
	/**
	 * Stop execution of tasks for this thread and also
	 * shutdown ScheduledExecutorService object connected with AbstractRetrievalThread.
	 * 
	 * @return - true on success, otherwise false.
	 */
	public abstract boolean killThread();
	
	/**
	 * Stop execution of tasks for this thread.
	 * 
	 * @return - true on success, otherwise false.
	 */
	public abstract boolean stopThread();
	
	/**
	 * Returns information about whether the mailbox is actually listening.
	 * 
	 * @return - true when mailbox is actually listening, otherwise false.
	 */
	public abstract boolean isRunning();

	
	/**
	 * Returns list of MailMessage objects which are wanted to be downloaded from server.
	 * 
	 * @return - list of MailMessage objects which are wanted to be downloaded from server.
	 */
	public List<MailMessage> getMessagesToDownload(){
		
		return _messagesToDownload;
	}
	
	/**
	 * Restart execution of tasks for mailbox.
	 * 
	 * @return - true on success, otherwise false.
	 */
	public abstract boolean restartThread();
	
	/**
	 * single thread scheduled executor service for managing retrieval tasks
	 */
	protected ScheduledExecutorService _executor;
	
	/**
	 * queue of id of messages to download
	 */
	protected List<MailMessage> _messagesToDownload;
	
	/**
	 * mail account which will be listening for new messages
	 */
	protected MailAccount _mailAccount;
	
	/**
	 * dataBase Manager which stores messages data on device
	 * it is strictly binded with concrete mail account - _mailAccount
	 */
	protected IncomingMailDBManager _dbManager;

	
	/**
	 * Application context
	 */
	protected Context _context;
	
	
}
